<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/12
 * Time: 11:43
 */

namespace app\api\controller;

use cmf\controller\HomeBaseController;
use think\Db;

/**
 * Class CaseController
 * @package app\api\controller
 * 一合案例模块
 */
class CaseController extends HomeBaseController
{
    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * number  每页条数
     * 案例列表
     */
    public function case_list()
    {
        //每页条数
        $number = request()->param('number');
        $id = Db::name('portal_category')->where('name','一合案例')->value('id');
        $ids = Db::name('portal_category_post')->where('category_id',$id)->column('post_id');
        $case_list = Db::name('portal_post')->where('id','in',$ids)->where('delete_time',0)->order('post_hits desc')->paginate($number);

        return_json(1,'获取数据成功',$case_list);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 案例详情
     */
    public function case_details()
    {
        $id = request()->param('id');

        $details = Db::name('portal_post' )->where('id',$id)->find();
        $details['post_content'] = htmlspecialchars_decode($details['post_content']);
        $details['published_time'] = date('Y-m-d H:i:s');

        return_json(1,'获取详情成功',$details);
    }


    /**
     * 上一篇 | 下一篇
     * name   类型名称
     * time    时间
     * type    类型   1为上一篇 | 2为下一篇
     */
    public function up_down(){
        $name = request()->param('name');
        $time = request()->param('time');
        $type = request()->param('type');

        $id = Db::name('portal_category')->where('name',$name)->value('id');
        $ids = Db::name('portal_category_post')->where('category_id',$id)->column('post_id');
        //上一篇
        if($type==1){
            $details = Db::name('portal_post')->where('id','in',$ids)->where('create_time','<',$time)->order('create_time desc')->limit(1)->find();
        }else{
            //下一篇
            $details = Db::name('portal_post')->where('id','in',$ids)->where('create_time','>',$time)->limit(1)->find();
        }
        $details['post_content'] = htmlspecialchars_decode($details['post_content']);
        $details['published_time'] = date('Y-m-d H:i:s');
        return_json(1,'获取数据成功',$details);
    }

}